package com.hotel.order.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hotel.order.pojo.Order;
import com.hotel.order.pojo.OrderResult;
import com.hotel.order.service.OrderService;
import com.hotel.order.utils.MyException;

@Controller
@RequestMapping("/order/manage")
public class OrderController {
	@Autowired
	private OrderService orderService;
	
	/**
	 * 后台系统添加订单
	 * @param order 订单信息 缺少id state默认给值为0 代表待入住 userId    
	 * @param userName 用户名 根据用户查找userid
	 * @return map封装成功或者失败的标识
	 */
	@RequestMapping("/addOrder")
	@ResponseBody
	public Map<Object,Object> addOrder(Order order,String userName) {
		Map<Object,Object> map = new HashMap<Object,Object>();
		try {
			orderService.addOrder(order,userName);
			map.put("success", true);
		}catch(MyException e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.getMessage());
		}catch(Exception e ) {
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 后台系统查看全部订单信息
	 * @return
	 */
	@RequestMapping("/selectAll")
	@ResponseBody
	public List<OrderResult> selectAllOrder() {
		try {
			return orderService.selectAllOrder();
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 后台系统删除订单
	 * @param id 订单的id
	 * @param roomType 房间类型
	 * @return
	 */
	@RequestMapping(value="/deleteOrder",produces="application/json;charset=utf-8")
	@ResponseBody
	public Map<Object,Object> deleteOrder(String id,String roomType) {
		Map<Object,Object> map = new HashMap<Object,Object>();
		try {
			orderService.deleteOrderById(id,roomType);
			map.put("success", true);
		}catch(Exception e ) {
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	//前台根据用户id查询订单
	@RequestMapping(value="/query" ,produces="application/json;charset=utf-8")
	@ResponseBody
	public List<Order> queryMyOrders(String userId){
		return orderService.queryMyOrders(userId);
		
	}
	
	//根据用户id删除订单
	@RequestMapping(value="/deleteOrderByUserId" ,produces="application/json;charset=utf-8")
	@ResponseBody
	public String deleteOrderByUserId(String userId){
		try{ orderService.deleteOrderByUserId(userId);
			return "success";
		}catch(Exception e){
			e.printStackTrace();
			return "fail";
		}
	}
}
